summaryrefslogtreecommitdiff
path: root/src/pages/api/tasks/[id].ts
diff options
context:
space:
mode:
authorYuval Adam <_@yuv.al>2025-06-27 13:14:00 +0200
committerYuval Adam <_@yuv.al>2025-06-27 13:14:00 +0200
commit72b63296438f469fc261562690495580ab8547d9 (patch)
tree5d6e7ed36b7c59b1371f0de3f7711496d2086348 /src/pages/api/tasks/[id].ts
parentbc750614c5780ff1f3e59d692492924ffe70edd6 (diff)
Extract common api stuffastro-base-cf
Diffstat (limited to 'src/pages/api/tasks/[id].ts')
-rw-r--r--src/pages/api/tasks/[id].ts43
1 files changed, 11 insertions, 32 deletions
diff --git a/src/pages/api/tasks/[id].ts b/src/pages/api/tasks/[id].ts
index a078d7c..55cc603 100644
--- a/src/pages/api/tasks/[id].ts
+++ b/src/pages/api/tasks/[id].ts
@@ -1,10 +1,11 @@
import type { APIRoute } from 'astro';
+import { jsonResponse, getDB, handleAPIError } from '../../../lib/api';
export const prerender = false;
export const PUT: APIRoute = async ({ params, request, locals }) => {
- try {
- const db = locals.runtime.env.DB;
+ return handleAPIError(async () => {
+ const db = getDB(locals);
const id = params.id;
const body = await request.json();
const { title, description, completed } = body;
@@ -14,46 +15,24 @@ export const PUT: APIRoute = async ({ params, request, locals }) => {
).bind(title, description || '', completed || false, id).all();
if (results.length === 0) {
- return new Response(JSON.stringify({ error: 'Task not found' }), {
- status: 404,
- headers: { 'Content-Type': 'application/json' }
- });
+ return jsonResponse({ error: 'Task not found' }, 404);
}
- return new Response(JSON.stringify(results[0]), {
- status: 200,
- headers: { 'Content-Type': 'application/json' }
- });
- } catch (error) {
- return new Response(JSON.stringify({ error: 'Failed to update task' }), {
- status: 500,
- headers: { 'Content-Type': 'application/json' }
- });
- }
+ return jsonResponse(results[0]);
+ });
};
export const DELETE: APIRoute = async ({ params, locals }) => {
- try {
- const db = locals.runtime.env.DB;
+ return handleAPIError(async () => {
+ const db = getDB(locals);
const id = params.id;
const { success } = await db.prepare('DELETE FROM tasks WHERE id = ?').bind(id).run();
if (!success) {
- return new Response(JSON.stringify({ error: 'Task not found' }), {
- status: 404,
- headers: { 'Content-Type': 'application/json' }
- });
+ return jsonResponse({ error: 'Task not found' }, 404);
}
- return new Response(JSON.stringify({ message: 'Task deleted successfully' }), {
- status: 200,
- headers: { 'Content-Type': 'application/json' }
- });
- } catch (error) {
- return new Response(JSON.stringify({ error: 'Failed to delete task' }), {
- status: 500,
- headers: { 'Content-Type': 'application/json' }
- });
- }
+ return jsonResponse({ message: 'Task deleted successfully' });
+ });
}; \ No newline at end of file